<?php
  class FileUtil{                      
      private static $array = array();
      
      public function __construct(){
          $this->array = array();
      }
      
      private static function initArray($dir){
          self::$array = array();
          return self::getFileList($dir);
      }
      
      /**
      * 检查指定文件夹里的文件读写权限，返回不可写的文件 
      * 
      * @param mixed $dir
      */
      public static function getUnwritableFile($dir){                             
          $array = self::initArray($dir);
          $rightArray = array();
          foreach($array as $temp){
              $filename = $temp['path'];
              $file_ext = pathinfo($filename,PATHINFO_EXTENSION);
              if(($file_ext == 'php')||($file_ext == 'xml')){
                  if(!is_writable($filename)){
                      $temp['name'] = realpath($filename);
                      $rightArray[] = $temp;
                  }
              }
          }
          return $rightArray;
      }
      
      /**
      * 获取文件夹大小
      * 
      * @param mixed $dir
      */
      public static function getDirSize($dir){
          $dir_size=0;                      
          $array = self::initArray($dir);
          foreach($array as $temp){
              $filesize = $temp['size'];
              $dir_size += $filesize;
          }
          return $dir_size;
      } 
      
      /**
      * 删除文件夹里的所有文件
      * 
      * @param mixed $dir
      */
      public static function deleteDir($dir){ 
          $array = self::initArray($dir);
          foreach($array as $temp){
              $filename = $temp['path'];
              unlink($filename);
          }
      }
      
      /**
      * 获取指定文件夹中的全部文件
      * 
      * @param mixed $dir
      */
      public static function getFileList($dir, $ext='*'){
          if(is_dir($dir)){
              $link = @opendir($dir);
              if(!$link){
                  return false;
              } 
              while($file = readdir($link)){
                  if($file!='.'&&$file!='..'){
                      $filename = $dir.'/'.$file;
                      if(is_file($filename)){
                          if($ext != '*'){
                              $file_ext = pathinfo($filename,PATHINFO_EXTENSION);
                              if($file_ext != $ext) continue;    
                          }                                                
                          $temp['name'] = $file;
                          $temp['path'] = $filename;     
                          $temp['size'] = filesize($filename);  
                          $temp['time'] = filectime($filename);     
                          self::$array[] = $temp;
                      }
                      if(is_dir($filename)){
                          self::getFileList($filename, $ext);
                      }
                  }
              }
              closedir($link);
              return self::$array;
          }
          else{
              return false;
          }
      }
  }
?>
